package com.wjs.juc.Atmoic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

class Bank{
    private String name="BBC";
    public volatile int money=0;

    AtomicIntegerFieldUpdater<Bank> fieldUpdater=AtomicIntegerFieldUpdater.newUpdater(Bank.class,"money");

    public void addPlus(Bank bank){
        fieldUpdater.getAndIncrement(bank);
    }
}
public class AtomicIntergerFieldUpdaterDemo {
    public static void main(String[] args) throws InterruptedException {
        Bank bank = new Bank();
        CountDownLatch countDownLatch = new CountDownLatch(50);
        for (int i = 0; i < 50; i++) {
            new Thread(()->{
                try {
                    for (int i1 = 0; i1 < 1000; i1++) {
                        bank.addPlus(bank);
                    }
                } finally {
                    countDownLatch.countDown();
                }
            }).start();
        }
        countDownLatch.await();
        System.out.println("结果为"+bank.money);
    }
}
